

<!-- 领券中心 -->
<template>
	<view>
		<!-- header部分 -->
		<view class='header-Container header-height'>
			<image class="header-bg" :src="icon.headerBg"></image>
			<navHeader pageTitle="领券中心" colorTheme="white">
			</navHeader>
		</view>
		
		<!-- 优惠券分类 -->
		<view class="navbar headerNext">
			<view class="navItem-Container">
				<view 
					v-for="(item, index) in navList" :key="index" 
					class="nav-item" 
					:class="{current: categoryId == item.id}"
					@click="categoryClick(index)"
				>
					{{item.name}}
				</view>
			</view>
			
			<view @click="toggleMoreNavbar" class="moreCategory">
				<image src="/static/images/user/helpService/downIcon.png" mode=""></image>
			</view>
		</view>
		<view v-if="showMoreNavbarFlag" class="moreNavbar-Container">
			<view
				v-for="(item, index) in navList" :key="index" 
				class="nav-item" 
				:class="{current: categoryId == item.id}"
				@click="showMoreNavbarFlag = false; categoryClick(index)"
			>
				{{item.name}}
			</view>
		</view>
		
		<!-- 优惠券列表 -->
		<view v-if="couponList && couponList.length" class="list-scroll-content">
			<view class="coupon" v-for="(item, index) in couponList" :key="item.id">
				<image v-if="item.select" class="bg" :src="icon.youhui_zk"></image>
				<image v-else class="bg" :src="icon.youhuiquan"></image>
				
				<view class="detail">
					<view class="left">
						<view v-if="item.couponType != 20" class="price">
							<text>￥</text>
							<text>{{item.faceValue / 100}}</text>
						</view>
						<view v-else class="price">
							<text style="font-size: 54rpx;">{{ item.faceValue / 10 }}</text>
							<text style="font-size: 28rpx;">折</text>
						</view>
						
						<view class="msg ellipsis2">
							满￥{{ item.enoughMoney / 100 }}可用
						</view>
					</view>
					<view class="right">
						<view class="top">
							<view class="name ellipsis2">
								
								<!-- 10满减券,20折扣券,30免单券,40免运费券,50满减运费券 -->
								<text class="quan" v-if="item.couponType == 10">满减券</text>
								<text class="quan" v-else-if="item.couponType == 20">折扣券</text>
								<text class="quan" v-else-if="item.couponType == 30">免单券</text>
								<text class="quan" v-else-if="item.couponType == 40">免运费券</text>
								<text class="quan" v-else-if="item.couponType == 50">满减运费券</text>
								
								<!-- 优惠券名称 -->
								<text class="text">{{ item.couponName }}</text>
							</view>
							<view class="time">
								<view class="canUse">
									{{item.beginTime | formatDate}}-{{item.endTime | formatDate}}
								</view>
								
								<view class="use">
									<!-- 是否已领取： 0 未领取 1已领取 -->
									<view @click="addCouponHandler(index)" v-if="item.receiveFlag == 0">立即领取</view>
									<view @click="gotoCategoryPage()" v-if="item.receiveFlag == 1">去使用</view>
								</view>
							</view>
						</view>
						<view class="bottom" @click="showUseDescHandler(index)">
							<view>使用说明</view>
							<view>
								<image v-if="!item.select" :src="icon.down"></image>
								<image v-if="item.select" :src="icon.up"></image>
							</view>
						</view>
					</view>
				</view>
				
				<view v-if="item.select" class="discount-detail">
				   {{item.ruleDes}}
				</view>
			</view>
		</view>
		<g-empty v-else></g-empty>
	</view>
</template>

<script>
import navHeader from '@/components/nav-header/nav-header';
export default {
	components: {
		navHeader
	},
	filters: {
		formatDate: function(date) {
			
			var date = new Date(date);
			var YY = date.getFullYear() + '-';
			var MM = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
			var DD = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate());
			// var hh = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
			// var mm = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
			// var ss = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
			
			return YY + MM + DD;
		}
	},
	data() {
		return {
			icon:{
				headerBg: "/static/images/common/header.png",
				return:'/static/images/common/return-white.png',
				top:'/static/images/common/header.png',
				msgCenterWhite:'/static/images/common/msg-center-white.png',
				
				youhuiquan:'/static/images/coupon/youhuiquan.png',
				youhui_zk:'/static/images/coupon/youhui_zk.png',
				down:'/static/images/coupon/down.png',
				up:'/static/images/coupon/up.png'
			},
			
			//分类
			showMoreNavbarFlag: false,
			navList: [],
			
			//优惠券列表
			couponList: [],
			
			//当前分类
			categoryId: "",
			
			
			//分页相关
			pageNum: 1,
			pageSize: 10
		};
	},
	onLoad(option) {
		//优惠券分类
		this.getCouponNavList();
	},
	methods: {
		
		//获取优惠券分类
		getCouponNavList() {
			
			var that = this;
			that.$api.globalUtil.requestAjax({
				url: '/coupon/getCouponCenterCategoryList',
				method: "POST",
				success: function(res) {
					if(res.data.success) {
						that.navList = res.data.response;
						if(that.navList && that.navList.length) {
							that.categoryId = that.navList[0].id;
							that.getCouponListByNav();
						}
					}
				},
			});
		},
		
		//分类点击
		categoryClick(index){
			let that = this;
			this.showMoreNavbarFlag = false;
			that.categoryId = that.navList[index].id;
			that.pageNum = 1;
			this.couponList = [];
			this.getCouponListByNav(this.navList[index].id);
		},
		// 优惠券列表
		getCouponListByNav() {
			
			var that = this;
			that.$api.globalUtil.requestAjax({
				url: '/coupon/getCouponCenterList',
				method: "POST",
				data: {
					categoryId: that.categoryId,
					pageNum: that.pageNum,
					pageSize: that.pageSize
				},
				success: function(res) {
					if(res.data.success) {
						if(res.data.response.results.length) {
							if(that.pageNum == 1) {
								that.couponList = res.data.response.results;
							}else {
								that.couponList = that.couponList.concat(res.data.response.results);
							}
						}else {
							that.$api.msg("没有更多数据！");
						}
					}
				},
			});
		},
		
		
		//使用说明展开/收缩
		showUseDescHandler(index) {
			this.$set(this.couponList[index], "select", !this.couponList[index].select); 	
		},
		
		
		//领取优惠券
		addCouponHandler(index) {
			var that = this;
			that.$api.globalUtil.requestAjax({
				url: '/coupon/addCustomerCoupon',
				method: "POST",
				data: {
					couponId: that.couponList[index].couponId
				},
				success: function(res) {
					if(res.data.success) {
						that.$api.msg("领取成功")
						that.$set(that.couponList[index], "receiveFlag", 1); 	
					}
				},
			});
		},
		
		
		//toggle更多分类
		toggleMoreNavbar() {
			this.showMoreNavbarFlag = !this.showMoreNavbarFlag;
		},
		
		
		//分类页面
		gotoCategoryPage() {
			uni.switchTab({
				url: '/pages/category/category'
			});
		}
	},
	onPullDownRefresh() {
		var that = this;
		that.pageNum = 1;
		
		setTimeout(function () {
			uni.stopPullDownRefresh();
		}, 1500);
		
		//优惠券分类
		this.getCouponNavList();
	},
	onReachBottom() {
		var that = this;
		that.pageNum = that.pageNum + 1;
		
		setTimeout(function () {
			uni.stopPullDownRefresh();
		}, 1500);
		
		this.getCouponListByNav();
	}
};
</script>

<style lang="scss">
	
	page{
		background: #F5F5F5;
	}
	
	// header部分
	.header-Container {
		// position: relative;
		// width: 100%;
		height: 176rpx;
		overflow: hidden;
		box-shadow: 0px 1px 1px 0px rgba(0, 0, 0, 0.08);
		background: rgba(255, 255, 255, 1);
		
		.header-bg {
			position: absolute;
			top: 0;
			left: 0;
			width: 100%;
			height: 100%;
			background:#FE5E49;
		}
	
		.leftBtns {
			display: flex;
			align-items: center;
			justify-content: flex-start;
			position: absolute;
			bottom: 0;
			width: 100%;
			height: 86rpx;
	
			.back {
				margin-left: 17rpx;
				width: 44rpx;
				height: 44rpx;
			}
		}
	
		.title {
			position: absolute;
			bottom: 0;
			width: 100%;
			height: 86rpx;
			line-height: 86rpx;
			text-align: center;
			font-size: 36rpx;
			font-family: PingFang SC;
			font-weight: bold;
			color: rgba(51, 51, 51, 1);
		}
	}
	
	
	//优惠券分类
	.navbar{
		display: flex;
		height: 80rpx;
		background: #fff;
		box-shadow: 0 1px 5px rgba(0,0,0,.06);
		position: sticky;
		z-index: 10;
		overflow: hidden;
		
		.nav-item{
			flex: 1;
			display: flex;
			justify-content: center;
			align-items: center;
			position: relative;
			height: 100%;
			font-size:24rpx;
			font-family:PingFang;
			font-weight:500;
			color:rgba(51,51,51,1);
			
			&.current{
				font-weight:bold;
				color:rgba(51,51,51,1);
				&:after{
					content: '';
					position: absolute;
					left: 50%;
					bottom: 12rpx;
					transform: translateX(-50%);
					width: 44px;
					height: 0;
					width: 50rpx;
					height: 4rpx;
					background:#FE5E49;
					border-radius:2px;
				}
			}
		}
		
		.moreCategory {
			display: flex;
			align-items: center;
			justify-content: center;
			position: absolute;
			right: 0;
			margin-left: auto;
			width: 80rpx;
			height: 80rpx;
			
			image {
				width: 21rpx;
				height: 12rpx;
			}
		}
	}
	
	
	// 更多分类容器
	.moreNavbar-Container {
		display: flex;
		padding: 26rpx 30rpx;
		width: 100%;
		background:rgba(255,255,255,1);
		border-radius:0px 0px 16rpx 16rpx;
		
		.nav-item {
			margin-right: 20rpx;
			padding: 0 15rpx;
			display: inline-block;
			height:36rpx;
			line-height: 36rpx;
			text-align: center;
			font-size:20rpx;
			font-family:PingFang;
			// font-weight:500;
			color:rgba(102,102,102,1);
			background:rgba(238,238,238,1);
			border:1rpx solid rgba(229,229,229,1);
			border-radius:18rpx;
		}
		
		.current {
			color: #FE5E49;
			background:rgba(238,238,238,1);
			border:1rpx solid #FE5E49;
		}
	}
	
	
	// 单个优惠券
	.coupon { 
		margin: 0 auto;
		position: relative;
		width: 690rpx;
		// height: 190rpx;
		z-index: 2;
		margin-top: 20rpx;
		.bg {
			height: 190rpx;
			width: 690rpx;
			position: absolute;
			z-index: 1;
			top: 0;
		}
		.detail {
			position: relative;
			z-index: 2;
			height: 190rpx;
			width: 690rpx;
			display: flex;
			.left {
				text-align: center;
				padding-top: 50rpx;
				margin-left: 6rpx;
				width: 199rpx;
				height: 190rpx;
				.price text:first-child {
					color: #FE5E49;
					font-size: 30rpx;
					font-weight: bold;
				}
				.price text:last-child {
					color: #FE5E49;
					font-size: 54rpx;
					font-weight: bold;
				}
				.msg {
					font-size: 20rpx;
					color: #FE5E49;
					margin-top: -6rpx;
				}
			}
			.right {
				width: 488rpx;
				height: 190rpx;
				padding-top: 12rpx;
				.top {
					padding: 0 29rpx 16rpx 30rpx;
					line-height: 40rpx;
					border-bottom: 1rpx solid #dbdbdb;
					border-bottom-style: dotted;
					.name {
						height: 88rpx;
					}
					.text {
						font-size: 22rpx;
						font-weight: bold;
						color: #333333;
					}
					.quan {
						display: inline-block;
						height: 26rpx;
						line-height: 26rpx;
						font-size: 18rpx;
						color: #FE5E49;
						border-radius: 13rpx;
						border: 1px solid #FE5E49;
						padding: 0 14rpx 0 14rpx;
						margin-right: 19rpx;
					}
					.time {
						height: 36rpx;
						width: 100%;
						display: flex;
						align-items: center;
						.canUse {
							color: #666666;
							width: 70%;
							font-size: 20rpx;
						}
						.use {
							width: 30%;
							text-align: right;
							view {
								color: #fff;
								width: 120rpx;
								border-radius: 18rpx;
								height: 36rpx;
								display: flex;
								align-items: center;
								justify-content: center;
								font-size: 18rpx;
								background: #FE5E49;
							}
						}
					}
				}
				.bottom {
					display: flex;
					align-items: center;
					height: 40rpx;
					image {
						width: 12rpx;
						height: 8rpx;
					}
					view:first-child {
						color: #666666;
						font-size: 18rpx;
						padding-left: 26rpx;
						width: 50%;
					}
					view:last-child {
						padding-right: 26rpx;
						width: 50%;
						display: flex;
						align-items: center;
						flex-direction: row-reverse;
					}
				}
			}
		}
	}
	// 优惠券使用说明
	.discount-detail{
		background:rgba(255,255,255,1);
		border:1rpx solid rgba(255,229,231,1);
		border-top: 0;
		box-shadow:0px 3rpx 6rpx 0px rgba(152,152,152,0.1);
		border-radius:0 0 10rpx 10rpx;
		padding: 10rpx 20rpx;
		width: 100%;
		font-size:18rpx;
		color:rgba(102,102,102,1);
	}
</style>



